package com.lu.utils;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.cluster.Health;
import io.searchbox.cluster.NodesInfo;
import io.searchbox.cluster.NodesStats;
import io.searchbox.indices.ClearCache;
import io.searchbox.indices.Flush;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @program Elasticsearch
 * @description: es jest 其他 工具
 * @author: zhanglu
 * @create: 2020-04-30 08:50:00
 */
@Setter
@Component
public class JestOtherUtil {

    @Autowired
    private JestClient jestClient;

    /**
     * 刷新索引
     * @throws Exception
     */
    public JestResult flush() throws Exception {
        Flush flush = new Flush.Builder().build();
        return jestClient.execute(flush);
    }

    /**
     * 清缓存
     * @throws Exception
     */
    public JestResult clearCache() throws Exception {
        ClearCache closeIndex = new ClearCache.Builder().build();
        return jestClient.execute(closeIndex);
    }

    /**
     * 查看节点信息
     * @throws Exception
     */
    public JestResult nodesInfo() throws Exception {
        NodesInfo nodesInfo = new NodesInfo.Builder().build();
        return jestClient.execute(nodesInfo);
    }


    /**
     * 查看集群健康信息
     * @throws Exception
     */
    public JestResult health() throws Exception {
        Health health = new Health.Builder().build();
        return jestClient.execute(health);
    }

    /**
     * 节点状态
     * @throws Exception
     */
    public JestResult nodesStats() throws Exception {
        NodesStats nodesStats = new NodesStats.Builder().build();
        return jestClient.execute(nodesStats);
    }

}
